//
//  TNAddAccountHeadView.m
//  TravelNotes
//
//  Created by 黄锋 on 15/8/14.
//  Copyright (c) 2015年 王浩宇. All rights reserved.
//

#import "TNAddAccountHeadView.h"
#import "Macro.h"
#import "TNToolCabinetCollectionViewCell.h"

@interface TNAddAccountHeadView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITextFieldDelegate>

@property(nonatomic, strong) NSArray *titleArr;
@property(nonatomic, strong) NSArray *imageArr;

@end

@implementation TNAddAccountHeadView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _titleArr = @[@"其它", @"交通", @"餐饮", @"住宿", @"门票", @"购物", @"娱乐"];
        _imageArr = @[@"More", @"CategoryIcon3",@"CategoryIcon7", @"CategoryIcon6", @"CategoryIcon4", @"NodeIconShopping25nb", @"PlanShowIcon10"];
        _tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 40)];
        _tf.font = [UIFont fontWithName:@"Helvetica" size:30];
        _tf.placeholder = @"输入消费金额";
        _tf.textAlignment = NSTextAlignmentRight;
        _tf.keyboardType = UIKeyboardTypeDecimalPad;
        _tf.borderStyle = UITextBorderStyleRoundedRect;
        _tf.delegate = self;
        [self addSubview:_tf];
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
        _cv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_WIDTH / 2) collectionViewLayout:fl];
        _cv.backgroundColor = [UIColor whiteColor];
        _cv.delegate = self;
        _cv.dataSource = self;
        [_cv registerClass:[TNToolCabinetCollectionViewCell class] forCellWithReuseIdentifier:@"TNToolCabinetCollectionViewCell"];
        [self addSubview:_cv];
    }
    return self;
}

#pragma mark - collectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH / 5, SCREEN_WIDTH / 5);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TNToolCabinetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TNToolCabinetCollectionViewCell" forIndexPath:indexPath];
    cell.pic.image = [UIImage imageNamed:_imageArr[indexPath.item]];
    cell.lable.text = _titleArr[indexPath.item];
    if (_index == indexPath.item) {
        cell.backgroundColor = [UIColor greenColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_delegate && [_delegate respondsToSelector:@selector(seleteItem)]) {
        [_delegate seleteItem];
    }
    if (_index != indexPath.item) {
        TNToolCabinetCollectionViewCell *cell = (TNToolCabinetCollectionViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_index inSection:0]];
        cell.backgroundColor = [UIColor whiteColor];
        _index = indexPath.item;
        cell = (TNToolCabinetCollectionViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_index inSection:0]];
        cell.backgroundColor = [UIColor greenColor];
    }
}

- (void)setIndex:(NSInteger)index
{
    [self collectionView:_cv didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(beginE)]) {
        [_delegate beginE];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(endE)]) {
        [_delegate endE];
    }
    return YES;
}

@end
